How do i route TCP connections via TOR? [on hold]

Posted by acidzombie24 on Server Fault See other posts from Server Fault or by acidzombie24
Published on 2012-07-09T16:11:19Z Indexed on 2013/10/22 21:56 UTC
Read the original article Hit count: 186

Filed under:
|
|
|

I was reading about torchat which is essentially an anonymous chat program.

It sounded cool so i wanted to experiment with making my own. First i wrote a test to grab a webpage using Http. Sicne .NET doesnt support SOCKS4A/SOCKS5 i used privoxy and my app worked. Then i switch to a TCP echo test and privoxy doesnt support TCP so i searched and installed 6+ proxy apps (freecap, socat, freeproxy, delegate are the ones i can remember from the top of my head, i also played with putty bc i know it supports tunnels and SOCK5) but i couldnt successfully get any of them to work let alone get it running with my http test that privoxy easily and painlessly did.

What may i use to get TCP connections going through TOR? I spent more then 2 hours without success. I don't know if i am looking for a relay, tunnel, forwarder, proxy or a proxychain which all came up in my search. I use the config below for .NET. I need TCP working but i am first testing with http since i know i had it working using privoxy. What apps and configs do i use to get TCP going through tor?

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.net>
    <defaultProxy enabled="true">
      <proxy bypassonlocal="True" proxyaddress="http://127.0.0.1:8118"/>
    </defaultProxy>
    <settings>
      <httpWebRequest useUnsafeHeaderParsing="true"/>
    </settings>
  </system.net>
</configuration>

-edit- Thanks to Bernd i have a solution. Here is the code i ended up writing. It isn't amazing but its fair.

static NetworkStream ConnectSocksProxy(string proxyDomain, short proxyPort, string host, short hostPort, TcpClient tc)
{
    tc.Connect(proxyDomain, proxyPort);
    if (System.Text.RegularExpressions.Regex.IsMatch(host, @"[\:/\\]"))
        throw new Exception("Invalid Host name. Use FQDN such as www.google.com. Do not have http, a port or / in it");
    NetworkStream ns = tc.GetStream();
    var HostNameBuf = new ASCIIEncoding().GetBytes(host);
    var HostPortBuf = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(hostPort));
    if (true) //5
    {
        var bufout = new byte[128];
        var buflen = 0;
        ns.Write(new byte[] { 5, 1, 0 }, 0, 3);
        buflen = ns.Read(bufout, 0, bufout.Length);
        if (buflen != 2 || bufout[0] != 5 || bufout[1] != 0)
            throw new Exception();

        var buf = new byte[] { 5, 1, 0, 3, (byte)HostNameBuf.Length };
        var mem = new MemoryStream();
        mem.Write(buf, 0, buf.Length);
        mem.Write(HostNameBuf, 0, HostNameBuf.Length);
        mem.Write(new byte[] { HostPortBuf[0], HostPortBuf[1] }, 0, 2);
        var memarr = mem.ToArray();
        ns.Write(memarr, 0, memarr.Length);
        buflen = ns.Read(bufout, 0, bufout.Length);
        if (bufout[0] != 5 || bufout[1] != 0)
            throw new Exception();
    }
    else //4a
    {
        var bufout = new byte[128];
        var buflen = 0;
        var mem = new MemoryStream();
        mem.WriteByte(4);
        mem.WriteByte(1);
        mem.Write(HostPortBuf, 0, 2);
        mem.Write(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(1)), 0, 4);
        mem.WriteByte(0);
        mem.Write(HostNameBuf, 0, HostNameBuf.Length);
        mem.WriteByte(0);
        var memarr = mem.ToArray();
        ns.Write(memarr, 0, memarr.Length);
        buflen = ns.Read(bufout, 0, bufout.Length);
        if (buflen != 8 || bufout[0] != 0 || bufout[1] != 90)
            throw new Exception();
    }
    return ns;
}

Usage

using (TcpClient client = new TcpClient())
using (var ns = ConnectSocksProxy("127.0.0.1", 9050, "website.com", 80, client)) {...}

© Server Fault or respective owner

Related posts about proxy

Related posts about tcp